<?php
namespace app\common;

use think\facade\Request;
use think\Model;

/**
 * 系统模型基类
 * Class AppModel
 * @package app
 */
abstract class AppModel extends Model
{
    /**
     * @desc 列表查询
     * @param array $where
     * @param string $field
     * @param string $order
     * @param int $limit
     * @return array
     */
    public static function getList($where = [], $field = '*', $order = '', $offset = 0, $length = null)
    {
        if (!empty($length)) {
            $result = self::where($where)->field($field)->order($order)->limit($offset, $length)->select()->toArray();
        } else {
            $result = self::where($where)->field($field)->order($order)->select()->toArray();
        }
        return $result;
    }

    /**
     * @desc 分页查询
     * @param array $where
     * @param string $field
     * @param string $order
     * @return array
     */
    public static function getPageList($where = [], $field = '*', $order = '', $defaultPage = null, $defaultLimit = null)
    {
        // 统一处理分页参数
        $request = Request::param();
        $page = $defaultPage ? $defaultPage : ($request['page'] ?? 1);
        $limit = $defaultLimit ? $defaultLimit : ($request['limit'] ?? 10);

        // 获取分页数据
        $pageList = self::where($where)->field($field)->order($order)->paginate($limit, false, ['page' => $page])->toArray();

        // 返回数据和总数
        $return['data'] = $pageList['data'];
        $return['count'] = $pageList['total'];
        return $return;
    }

    /**
     * @desc 根据ID获取一条数据
     * @param $id
     * @return array
     */
    public static function getById($id)
    {
        $result = self::find($id);
        if (empty($result))
        {
            return [];
        }
        return $result->toArray();
    }

    /**
     * @desc 返回总数
     * @param array $where
     * @param string $field
     */
    public static function getCount($where = [], $field = 'id')
    {
        return self::where($where)->count($field);
    }

    /**
     * @desc 根据条件获取一条数据
     * @param array $where
     * @param string $field
     * @return array
     */
    public static function getOne($where = [], $field = '*')
    {
        $rt = self::where($where)->field($field)->find();
        if (empty($rt))
        {
            return [];
        }
        return $rt->toArray();
    }

    /**
     * @desc 获取某个列的数组
     * @param array $where
     * @param string $field
     * @param string $key
     * @return array
     */
    public static function getColumn($where = [], $field = '', $key = '')
    {
        return self::where($where)->column($field, $key);
    }

    /**
     * @desc 获取某个字段值
     * @param array $where
     * @param string $field
     * @param null $default
     * @return mixed
     */
    public static function getVal($where = [], $field = '', $default = null)
    {
        return self::where($where)->value($field);
    }

    /**
     * @desc 新增数据
     * @param array $data
     * @param bool $is_return_pk
     * @return mixed
     */
    public static function addOne($data = [], $is_return_pk = true)
    {
        return self::insert($data, $is_return_pk);
    }

    /**
     * @desc 更新数据
     * @param array $data
     * @param array $where
     * @param array $options 只更新指定字段
     * @return BaseModel|mixed
     */
    public static function updateOne($data = [], $where = [], $options = [])
    {
        return self::update($data, $where, $options);
    }

    /**
     * @desc 自增数据
     * @param array $where
     * @param $field
     * @param int $num
     * @return mixed
     */
    public static function setInc($where = [], $field, $num = 1)
    {
        return self::where($where)->inc($field, $num)->update();
    }

    /**
     * @desc 自减数据
     * @param array $where
     * @param $field
     * @param int $num
     * @return mixed
     */
    public static function setDec($where = [], $field, $num = 1)
    {
        return self::where($where)->dec($field, $num)->update();
    }

    /**
     * @desc 只提供ID字段的删除
     * @param $ids
     * @return bool
     */
    public static function deleteByIds($ids)
    {
        if (is_array($ids)) {
            return self::where([ ['id', 'in', $ids] ])->delete();
        } elseif (is_numeric($ids)) {
            return self::find($ids)->delete();
        } else {
            return true;
        }
    }
}
